REM PAR_din=15'12
REM PAR_D=35'22
REM PAR_B=18'10
REM PAR_C=14'7
REM PAR_d2=27'18
REM PAR_r1=0.5'0.3
REM PAR_r=1'0.6
REM version=2'3
REM PAR_AXIS=1
REM hatchStep=1
REM rule=0

REM GCV_FRONT=1
REM GCV_LEFT=4
REM SIDE=GCV_LEFT'GCV_FRONT


din=PAR_din
D=PAR_D
B=PAR_B
C=PAR_C
d2=PAR_d2
r1=PAR_r1
r=PAR_r
hatchAngle=45

bg=B*0.2 'groove diameter
dh=bg*0.25 'hole diameter
ycbgInRIn=din/2-bg/3
ycbgInROut=d2/2+bg/3
ycbgOutROut=D/2+bg/3

If B<20 Then ax=2 Else If B<50 Then ax=3 Else If B<100 Then ax=4 Else ax=5

Const POINT_ARR = 2
Const ko_MathPointParam = 14
Const ko_EllipseArcParam=23
Dim math, iDA, iPoint


Set math   = Kompas.GetMathematic2D
Set iDA    = Kompas.GetDynamicArray(POINT_ARR)
Set iPoint = Kompas.GetParamStruct(ko_MathPointParam)
Set elpc = Kompas.GetParamStruct(kO_EllipseArcParam)

Const pi=3.141592653589793
Dim d1,yCL,d1S,d2S,ySL,rc,xrc,yrc,xLbgInRIn,xRbgInRIn,xLbgInROutUp,yLbgInROutUp,xRbgInROutUp,yRbgInROutUp,xLbgInROutB,yLbgInROutB,xRbgInROutB,yRbgInROutB,xLbgOutROut,xRbgOutROut
Dim B4,l,yLbgdhInRIn,yRbgdhInRIn,yLbgdhInROut,yRbgdhInROut,yLbgdhOutROut,yRbgdhOutROut,yLbgdhDnOutROut,yRbgdhDnOutROut
Dim xCyCL,yCyCL,xrcCL,yrcCL,xrcSpH,yrcSpH,xrcSpV,yrcSpV,xD_W,yD_W,xDr_W,yDr_W,xd2_W,yd2_W,xCL_W,yCL_W,xd1_W,yd1_W,xdinr_W,ydinr_W,xdin_W,ydin_W,xbgIn_W,ybgIn_W,xbgOut_W,ybgOut_W,xbgInOut_W,ybgInOut_W,xdhIn_H,ydhIn_H,xdhInOut_H,ydhInOut_H,xd2_H,yd2_H,xdhOut_H,ydhOut_H,xdin_H,ydin_H,xD_H,yD_H

Sub CharacterPoints
'Bottom part
'crossing ark d2 with left edge inner ring, calculation d1
GetIntersectArcLin B/2,0,d2/2,0,180,1,0,0,90,d1x,d1y
d1=d1y*2
REM Doc.ksPoint d1x,d1y,0
'crossing ark d2 with left edge outer ring
GetIntersectArcLin B/2,0,d2/2,0,180,1,B/2-C/2,0,90,xCL,yCL
REM Doc.ksPoint xCL,yCL,0
'Upper part
'crossing ark spot with left edge inner ring, calculation d1
GetIntersectArcLin B/2,0,d2*0.4899,0,180,1,0,0,90,d1Sx,d1Sy
REM Doc.ksPoint d1Sx,d1Sy,0
d1S=d1Sy*2
'crossing ark spot d2*0.4899 with left edge outer ring
GetIntersectArcLin B/2,0,d2*0.4899,0,180,1,B/2-C/2,0,90,xSL,ySL
REM Doc.ksPoint xSL,ySL,0
d2S=d2*0.4899*2
'diameter of side cut when x=C/4
rc=(d2*sqr(1-C^2/(4*d2^2)))/2
'crossing ark spot with side cut projection
GetIntersectArcLin B/2,0,d2*0.4899,0,180,1,B/2-C/4,0,90,xrc,yrc
REM Doc.ksLine 0,rc,0
'inner ring inner groove upper and bottom part
GetIntersectArcLin B/2,ycbgInRIn,bg/2,90,270,1,0,din/2,0,xLbgInRIn,yLbgInRIn
xRbgInRIn=B-xLbgInRIn
yRbgInRIn=yLbgInRIn
REM Doc.ksPoint xLbgInRIn,yLbgInRIn,0
REM Doc.ksPoint xRbgInRIn,yRbgInRIn,0
REM Doc.ksCircle B/2,ycbgInRIn,bg/2,6
'inner ring outer groove upper part
GetIntersectArcArc B/2,ycbgInROut,bg/2,90,270,1,B/2,0,d2*0.4899,0,180,1,fArc,xLbgInROutUp,yLbgInROutUp
xRbgInROutUp=B-xLbgInROutUp
yRbgInROutUp=yLbgInROutUp
REM Doc.ksPoint xLbgInROutUp,yLbgInROutUp,0
REM Doc.ksPoint xRbgInROutUp,yRbgInROutUp,0
REM Doc.ksCircle B/2,ycbgInROut,bg/2,6
'inner ring outer groove bottom part
GetIntersectArcArc B/2,ycbgInROut,bg/2,90,270,1,B/2,0,d2/2,0,180,1,fArc,xLbgInROutB,yLbgInROutB
xRbgInROutB=B-xLbgInROutB
yRbgInROutB=yLbgInROutB
REM Doc.ksPoint xLbgInROutB,yLbgInROutB,0
REM Doc.ksPoint xRbgInROutB,yRbgInROutB,0
REM Doc.ksCircle B/2,ycbgInROut,bg/2,6
'outer ring outer groove upper and bottom part
GetIntersectArcLin B/2,ycbgOutROut,bg/2,90,270,1,0,D/2,0,xLbgOutROut,yLbgOutROut
xRbgOutROut=B-xLbgOutROut
yRbgOutROut=yLbgOutROut
REM Doc.ksPoint xLbgOutROut,yLbgOutROut,0
REM Doc.ksPoint xRbgOutROut,yRbgOutROut,0
REM Doc.ksCircle B/2,ycbgOutROut,bg/2,6
'*******************version 4******************************
REM alfad=5
REM alfa=alfad/180*pi
REM B4=C+(sin(alfa/2)*cos(asin(C/d2)-(pi-alfa)/2+pi/2)*d2)*2
REM kompas.ksMessage(B&"    "&B4)
'crossing inner ring inner groove (on din) profile with hole
GetIntersectArcLin B/2,ycbgInRIn,bg/2,90,180,1,B/2-dh/2,0,90,xLbgdhInRIn,yLbgdhInRIn
xRbgdhInRIn=B-xLbgdhInRIn
yRbgdhInRIn=yLbgdhInRIn
REM Doc.ksLine B/2-dh/2,0,90
REM Doc.ksPoint xLbgdhInRIn,yLbgdhInRIn,0
REM Doc.ksPoint xRbgdhInRIn,yRbgdhInRIn,0
REM Doc.ksCircle B/2,ycbgInRIn,bg/2,6
'crossing inner ring outer groove (on d2) profile with hole
GetIntersectArcLin B/2,ycbgInROut,bg/2,180,270,1,B/2-dh/2,0,90,xLbgdhInROut,yLbgdhInROut
xRbgdhInROut=B-xLbgdhInROut
yRbgdhInROut=yLbgdhInROut
REM Doc.ksPoint xLbgdhInROut,yLbgdhInROut,0
REM Doc.ksPoint xRbgdhInROut,yRbgdhInROut,0
REM Doc.ksCircle B/2,ycbgInROut,bg/2,6
'outer ring outer groove with hole
GetIntersectArcLin B/2,ycbgOutROut,bg/2,180,270,1,B/2-dh/2,0,90,xLbgdhOutROut,yLbgdhOutROut
xRbgdhOutROut=B-xLbgdhOutROut
yRbgdhOutROut=yLbgdhOutROut
REM Doc.ksPoint xLbgdhOutROut,yLbgdhOutROut,0
REM Doc.ksPoint xRbgdhOutROut,yRbgdhOutROut,0
REM Doc.ksCircle B/2,ycbgOutROut,bg/2,6
'hole with inside part of outer ring
GetIntersectArcLin B/2,0,d2/2,0,180,1,B/2-dh/2,0,90,xLbgdhDnOutROut,yLbgdhDnOutROut
xRbgdhDnOutROut=B-xLbgdhDnOutROut
yRbgdhDnOutROut=yLbgdhDnOutROut
REM Doc.ksPoint xLbgdhDnOutROut,yLbgdhDnOutROut,0
REM Doc.ksPoint xRbgdhDnOutROut,yRbgdhDnOutROut,0
REM Doc.ksCircle B/2,ycbgOutROut,bg/2,6
'*******************version 4******************************
'ooooooooooooooooooo Left ooooooooooooooooooooooooooooooooo
'Crossing circle yCL with line D*0.2
GetIntersectArcLin 0,0,yCL,0,90,1,D*0.2,0,90,xCyCL,yCyCL
REM Doc.ksPoint xCyCL,yCyCL,3
'Crossing circle rc with line D*0.2
GetIntersectArcLin 0,0,rc,0,90,1,D*0.2,0,90,xrcCL,yrcCL
REM Doc.ksPoint xrcCL,yrcCL,3
'Crossing ark rc with line d2s
GetIntersectArcLin 0,0,rc,0,90,1,0,d2s/2,0,xrcSpH,yrcSpH
REM Doc.ksPoint xrcSpH,yrcSpH,3
'Crossing ark rc with line d2*0.1
GetIntersectArcLin 0,0,rc,0,90,1,d2*0.1,0,90,xrcSpV,yrcSpV
REM Doc.ksPoint xrcSpV,yrcSpV,3

kw=-3.6e-6*B+0.105 'coefficient of waving
'Crossing ark D with wave line
GetIntersectArcLin 0,0,D/2,0,90,1,0,D*0.1,0,xD_W,yD_W
REM Doc.ksPoint xD_W,yD_W,3
'Crossing ark D with wave line
REM GetIntersectArcLin 0,0,D/2,0,90,1,0,D*0.11,0,xD_W,yD_W
REM Doc.ksPoint xD_W,yD_W,3
'Crossing ark D/2-r with wave line
GetIntersectArcLin 0,0,D/2-r,0,90,1,0,D*0.1,0,xDr_W,yDr_W
REM Doc.ksPoint xDr_W,yDr_W,3
'Crossing ark d2/2 with wave line
GetIntersectArcLin 0,0,d2/2,0,90,1,0,D*kw,0,xd2_W,yd2_W
REM GetIntersectArcLin 0,0,d2/2,0,90,1,0,D*0.1,0,xd2_W,yd2_W
REM GetIntersectArcLin 0,0,d2/2,0,90,1,0,D*(0.2-kw),0,xd2_W,yd2_W
REM Doc.ksPoint xd2_W,yd2_W,3
'Crossing ark yCL with wave line
GetIntersectArcLin 0,0,yCL,0,90,1,0,D*0.1,0,xCL_W,yCL_W
REM Doc.ksPoint xCL_W,yCL_W,3
'Crossing ark d1/2 with wave line
REM GetIntersectArcLin 0,0,d1/2,0,90,1,0,D*0.1,0,xd1_W,yd1_W
REM Doc.ksPoint xd1_W,yd1_W,3
'Crossing ark d1/2 with wave line
GetIntersectArcLin 0,0,d1/2,0,90,1,0,D*kw,0,xd1_W,yd1_W
REM Doc.ksPoint xd1_W,yd1_W,0
'Crossing ark din/2+r1 with wave line
GetIntersectArcLin 0,0,din/2+r1,0,90,1,0,D*0.1,0,xdinr_W,ydinr_W
REM Doc.ksPoint xdinr_W,ydinr_W,3
'Crossing ark din/2 with wave line
GetIntersectArcLin 0,0,din/2,0,90,1,0,D*0.1,0,xdin_W,ydin_W
REM Doc.ksPoint xdin_W,ydin_W,3
'Crossing ark ycbgInRIn+bg/2 with wave line
GetIntersectArcLin 0,0,ycbgInRIn+bg/2,0,90,1,0,D*0.1,0,xbgIn_W,ybgIn_W
REM Doc.ksPoint xbgIn_W,ybgIn_W,3
'Crossing ark ycbgInRIn+bg/2 with wave line
GetIntersectArcLin 0,0,ycbgOutROut-bg/2,0,90,1,0,D*0.1,0,xbgOut_W,ybgOut_W
REM Doc.ksPoint xbgOut_W,ybgOut_W,3
'Crossing ark ycbgInROut-bg/2 with wave line
GetIntersectArcLin 0,0,ycbgInROut-bg/2,0,90,1,0,D*0.1,0,xbgInOut_W,ybgInOut_W
REM GetIntersectArcLin 0,0,ycbgInROut-bg/2,0,90,1,0,D*(0.2-kw),0,xbgInOut_W,ybgInOut_W
REM Doc.ksPoint xbgInOut_W,ybgInOut_W,3

'Crossing ark ycbgInRIn+bg/2 with hole
GetIntersectArcLin 0,0,ycbgInRIn+bg/2,0,90,1,0,dh/2,0,xdhIn_H,ydhIn_H
REM Doc.ksPoint xdhIn_H,ydhIn_H,3
'Crossing ark ycbgInROut-bg/2 with hole
GetIntersectArcLin 0,0,ycbgInROut-bg/2,0,90,1,0,dh/2,0,xdhInOut_H,ydhInOut_H
REM Doc.ksPoint xdhInOut_H,ydhInOut_H,3
'Crossing ark d2/2 with Hole
GetIntersectArcLin 0,0,d2/2,0,90,1,0,dh/2,0,xd2_H,yd2_H
REM Doc.ksPoint xd2_H,yd2_H,0
'Crossing ark d2/2 with Hole
GetIntersectArcLin 0,0,ycbgOutROut-bg/2,0,90,1,0,dh/2,0,xdhOut_H,ydhOut_H
REM Doc.ksPoint xdhOut_H,ydhOut_H,0
'Crossing ark din/2 with Hole
GetIntersectArcLin 0,0,din/2,0,90,1,0,dh/2,0,xdin_H,ydin_H
REM Doc.ksPoint xdin_H,ydin_H,0
'Crossing ark D/2 with Hole
GetIntersectArcLin 0,0,D/2,0,90,1,0,dh/2,0,xD_H,yD_H
REM Doc.ksPoint xD_H,yD_H,0
'ooooooooooooooooooo Left ooooooooooooooooooooooooooooooooo
End Sub

Sub UpperHalf(kv)
'inner ring
Doc.ksLineSeg 0,(din/2+r1)*kv,0,d1S/2*kv,1
Doc.ksLineSeg B,(din/2+r1)*kv,B,d1S/2*kv,1
If version=1 Then
	Doc.ksArcByPoint B/2,0,d2S/2,B,d1S/2*kv,0,d1S/2*kv,1*kv,1
	Doc.ksLineSeg 0+r1,din/2*kv,B-r1,din/2*kv,1
Else
	'ark d2s with groove
	Doc.ksArcByPoint B/2,0,d2S/2,B,d1S/2*kv,xRbgInROutUp,yRbgInROutUp*kv,1*kv,1
	Doc.ksArcByPoint B/2,0,d2S/2,xLbgInROutUp,yLbgInROutUp*kv,0,d1S/2*kv,1*kv,1
	Doc.ksArcByPoint B/2,ycbgInROut*kv,bg/2,xLbgInROutUp,yLbgInROutUp*kv,xRbgInROutUp,yRbgInROutUp*kv,1*kv,1
	'line d with groove
	If rule Then
		Doc.ksLineSeg 0+r1,din/2*kv,xLbgInRIn,din/2*kv,1
		Doc.ksLineSeg xRbgInRIn,din/2*kv,B-r1,din/2*kv,1
		Doc.ksArcByPoint B/2,ycbgInRIn*kv,bg/2,xLbgInRIn,din/2*kv,xRbgInRIn,din/2*kv,-1*kv,1
		If PAR_AXIS Then
			Doc.ksLineSeg xLbgInRIn,0,xLbgInRIn,din/2*kv,1
			Doc.ksLineSeg xRbgInRIn,0,xRbgInRIn,din/2*kv,1
		End If
	Else
		Doc.ksLineSeg 0+r1,din/2*kv,B-r1,din/2*kv,1
	End If
End If
Doc.ksArcByPoint 0+r1,(din/2+r1)*kv,r1,0+r1,din/2*kv,0,(din/2+r1)*kv,-1*kv,1
Doc.ksArcByPoint B-r1,(din/2+r1)*kv,r1,B-r1,din/2*kv,B,(din/2+r1)*kv,1*kv,1
If PAR_AXIS Then
	Doc.ksLineSeg 0,0,0,(din/2+r1)*kv,1
	Doc.ksLineSeg B,0,B,(din/2+r1)*kv,1
	Doc.ksLineSeg 0+r1,0,0+r1,din/2*kv,1
	Doc.ksLineSeg B-r1,0,B-r1,din/2*kv,1
End If

'outer ring
Doc.ksLineSeg B/2-C/2,ySL*kv,B/2-C/2,(D/2-r)*kv,1
Doc.ksLineSeg B/2+C/2,ySL*kv,B/2+C/2,(D/2-r)*kv,1

If version<>3 OR rule=0 Then
	Doc.ksLineSeg B/2-C/2+r,D/2*kv,B/2+C/2-r,D/2*kv,1
Else
	Doc.ksLineSeg B/2-C/2+r,D/2*kv,xLbgOutROut,D/2*kv,1
	Doc.ksLineSeg xRbgOutROut,D/2*kv,B/2+C/2-r,D/2*kv,1
	Doc.ksArcByPoint B/2,ycbgOutROut*kv,bg/2,xLbgOutROut,D/2*kv,xRbgOutROut,D/2*kv,1*kv,1
End If

Doc.ksArcByPoint B/2,0,d2/2,B/2+C/2,yCL*kv,B/2-C/4,rc*kv,1*kv,1
Doc.ksLineSeg B/2-C/4,rc*kv,B/2-C/2,rc*kv,1
Doc.ksLineSeg B/2-C/4,rc*kv,B/2-C/4,yrc*kv,1

Doc.ksArcByPoint B/2-C/2+r,(D/2-r)*kv,r,B/2-C/2+r,D/2*kv,B/2-C/2,(D/2-r)*kv,1*kv,1
Doc.ksArcByPoint B/2+C/2-r,(D/2-r)*kv,r,B/2+C/2-r,D/2*kv,B/2+C/2,(D/2-r)*kv,-1*kv,1
End Sub

Sub BottomHalf(kv)
'inner ring
Doc.ksLineSeg 0,(din/2+r1)*kv,0,d1/2*kv,1
Doc.ksLineSeg B,(din/2+r1)*kv,B,d1/2*kv,1

If version=1 Then
	Doc.ksArcByPoint B/2,0,d2/2,B,d1/2*kv,0,d1/2*kv,1*kv,1
	Doc.ksLineSeg 0+r1,din/2*kv,B-r1,din/2*kv,1
Else
	'ark d2 with groove
	Doc.ksArcByPoint B/2,0,d2/2,B,d1/2*kv,0,d1/2*kv,1*kv,1
	Doc.ksArcByPoint B/2,ycbgInROut*kv,bg/2,xLbgInROutB,yLbgInROutB*kv,xRbgInROutB,yRbgInROutB*kv,1*kv,1
	'line d with groove
	If rule Then
		Doc.ksLineSeg 0+r1,din/2*kv,xLbgInRIn,din/2*kv,1
		Doc.ksLineSeg xRbgInRIn,din/2*kv,B-r1,din/2*kv,1
		Doc.ksArcByPoint B/2,ycbgInRIn*kv,bg/2,xLbgInRIn,din/2*kv,xRbgInRIn,din/2*kv,-1*kv,1
		If PAR_AXIS Then
			Doc.ksLineSeg xLbgInRIn,0,xLbgInRIn,din/2*kv,1
			Doc.ksLineSeg xRbgInRIn,0,xRbgInRIn,din/2*kv,1
		End If
	Else
		Doc.ksLineSeg 0+r1,din/2*kv,B-r1,din/2*kv,1
	End If
End If

Doc.ksArcByPoint 0+r1,(din/2+r1)*kv,r1,0+r1,din/2*kv,0,(din/2+r1)*kv,-1*kv,1
Doc.ksArcByPoint B-r1,(din/2+r1)*kv,r1,B-r1,din/2*kv,B,(din/2+r1)*kv,1*kv,1

If PAR_AXIS Then
	Doc.ksLineSeg 0,0,0,(din/2+r1)*kv,1
	Doc.ksLineSeg B,0,B,(din/2+r1)*kv,1
	Doc.ksLineSeg 0+r1,0,0+r1,din/2*kv,1
	Doc.ksLineSeg B-r1,0,B-r1,din/2*kv,1
End If

'outer ring
Doc.ksLineSeg B/2-C/2,yCL*kv,B/2-C/2,(D/2-r)*kv,1
Doc.ksLineSeg B/2+C/2,yCL*kv,B/2+C/2,(D/2-r)*kv,1
If version<>3 OR rule=0 Then
	Doc.ksLineSeg B/2-C/2+r,D/2*kv,B/2+C/2-r,D/2*kv,1
Else
	Doc.ksLineSeg B/2-C/2+r,D/2*kv,xLbgOutROut,D/2*kv,1
	Doc.ksLineSeg xRbgOutROut,D/2*kv,B/2+C/2-r,D/2*kv,1
	Doc.ksArcByPoint B/2,ycbgOutROut*kv,bg/2,xLbgOutROut,D/2*kv,xRbgOutROut,D/2*kv,1*kv,1
End If

Doc.ksArcByPoint B/2-C/2+r,(D/2-r)*kv,r,B/2-C/2+r,D/2*kv,B/2-C/2,(D/2-r)*kv,1*kv,1
Doc.ksArcByPoint B/2+C/2-r,(D/2-r)*kv,r,B/2+C/2-r,D/2*kv,B/2+C/2,(D/2-r)*kv,-1*kv,1
End Sub

'****************************************************
Sub UpperHalfOuterRingHatch(kv)
'outer ring
Doc.ksLineSeg B/2-C/2,rc*kv,B/2-C/2,(D/2-r)*kv,1
Doc.ksLineSeg B/2+C/2,yCL*kv,B/2+C/2,(D/2-r)*kv,1

If version<>3 OR rule=0 Then
	Doc.ksLineSeg B/2-C/2+r,D/2*kv,B/2+C/2-r,D/2*kv,1
Else
	Doc.ksLineSeg B/2-C/2+r,D/2*kv,xLbgOutROut,D/2*kv,1
	Doc.ksLineSeg xRbgOutROut,D/2*kv,B/2+C/2-r,D/2*kv,1
	Doc.ksArcByPoint B/2,ycbgOutROut*kv,bg/2,xLbgOutROut,D/2*kv,xRbgOutROut,D/2*kv,1*kv,1
End If

Doc.ksArcByPoint B/2,0,d2/2,B/2+C/2,yCL*kv,B/2-C/4,rc*kv,1*kv,1
Doc.ksLineSeg B/2-C/4,rc*kv,B/2-C/2,rc*kv,1

Doc.ksArcByPoint B/2-C/2+r,(D/2-r)*kv,r,B/2-C/2+r,D/2*kv,B/2-C/2,(D/2-r)*kv,1*kv,1
Doc.ksArcByPoint B/2+C/2-r,(D/2-r)*kv,r,B/2+C/2-r,D/2*kv,B/2+C/2,(D/2-r)*kv,-1*kv,1
End Sub

Sub BottomHalfOuterRingHatch(kv)
'outer ring
Doc.ksLineSeg B/2-C/2,yCL*kv,B/2-C/2,(D/2-r)*kv,1
Doc.ksLineSeg B/2+C/2,yCL*kv,B/2+C/2,(D/2-r)*kv,1
If version<>3 OR rule=0 Then
	Doc.ksLineSeg B/2-C/2+r,D/2*kv,B/2+C/2-r,D/2*kv,1
Else
	Doc.ksLineSeg B/2-C/2+r,D/2*kv,xLbgOutROut,D/2*kv,1
	Doc.ksLineSeg xRbgOutROut,D/2*kv,B/2+C/2-r,D/2*kv,1
	Doc.ksArcByPoint B/2,ycbgOutROut*kv,bg/2,xLbgOutROut,D/2*kv,xRbgOutROut,D/2*kv,1*kv,1
End If

Doc.ksArcByPoint B/2-C/2+r,(D/2-r)*kv,r,B/2-C/2+r,D/2*kv,B/2-C/2,(D/2-r)*kv,1*kv,1
Doc.ksArcByPoint B/2+C/2-r,(D/2-r)*kv,r,B/2+C/2-r,D/2*kv,B/2+C/2,(D/2-r)*kv,-1*kv,1
Doc.ksArcByPoint B/2,0,d2/2,B/2-C/2,yCL*kv,B/2+C/2,yCL*kv,-1*kv,1
End Sub


Sub UpperHalfInnerRingHatch(kv)
'inner ring
Doc.ksLineSeg 0,(din/2+r1)*kv,0,d1S/2*kv,1
Doc.ksLineSeg B,(din/2+r1)*kv,B,d1S/2*kv,1
If version=1 Then
	Doc.ksArcByPoint B/2,0,d2S/2,B,d1S/2*kv,0,d1S/2*kv,1*kv,1
	Doc.ksLineSeg 0+r1,din/2*kv,B-r1,din/2*kv,1
Else
	'ark d2s with groove
	Doc.ksArcByPoint B/2,0,d2S/2,B,d1S/2*kv,xRbgInROutUp,yRbgInROutUp*kv,1*kv,1
	Doc.ksArcByPoint B/2,0,d2S/2,xLbgInROutUp,yLbgInROutUp*kv,0,d1S/2*kv,1*kv,1
	Doc.ksArcByPoint B/2,ycbgInROut*kv,bg/2,xLbgInROutUp,yLbgInROutUp*kv,xRbgInROutUp,yRbgInROutUp*kv,1*kv,1
	'line d with groove
	If rule Then
		Doc.ksLineSeg 0+r1,din/2*kv,xLbgInRIn,din/2*kv,1
		Doc.ksLineSeg xRbgInRIn,din/2*kv,B-r1,din/2*kv,1
		Doc.ksArcByPoint B/2,ycbgInRIn*kv,bg/2,xLbgInRIn,din/2*kv,xRbgInRIn,din/2*kv,-1*kv,1
	Else
		Doc.ksLineSeg 0+r1,din/2*kv,B-r1,din/2*kv,1
	End If
End If
Doc.ksArcByPoint 0+r1,(din/2+r1)*kv,r1,0+r1,din/2*kv,0,(din/2+r1)*kv,-1*kv,1
Doc.ksArcByPoint B-r1,(din/2+r1)*kv,r1,B-r1,din/2*kv,B,(din/2+r1)*kv,1*kv,1
End Sub

Sub BottomHalfInnerRingHatch(kv)
'inner ring
Doc.ksLineSeg 0,(din/2+r1)*kv,0,d1/2*kv,1
Doc.ksLineSeg B,(din/2+r1)*kv,B,d1/2*kv,1

If version=1 Then
	Doc.ksArcByPoint B/2,0,d2/2,B,d1/2*kv,0,d1/2*kv,1*kv,1
	Doc.ksLineSeg 0+r1,din/2*kv,B-r1,din/2*kv,1
Else
	'ark d2 with groove
	Doc.ksArcByPoint B/2,0,d2/2,B,d1/2*kv,xRbgInROutB,yRbgInROutB*kv,1*kv,1
	Doc.ksArcByPoint B/2,0,d2/2,xLbgInROutB,yLbgInROutB*kv,0,d1/2*kv,1*kv,1
	Doc.ksArcByPoint B/2,ycbgInROut*kv,bg/2,xLbgInROutB,yLbgInROutB*kv,xRbgInROutB,yRbgInROutB*kv,1*kv,1
	'line d with groove
	If rule Then
		Doc.ksLineSeg 0+r1,din/2*kv,xLbgInRIn,din/2*kv,1
		Doc.ksLineSeg xRbgInRIn,din/2*kv,B-r1,din/2*kv,1
		Doc.ksArcByPoint B/2,ycbgInRIn*kv,bg/2,xLbgInRIn,din/2*kv,xRbgInRIn,din/2*kv,-1*kv,1
	Else
		Doc.ksLineSeg 0+r1,din/2*kv,B-r1,din/2*kv,1
	End If
	
End If

Doc.ksArcByPoint 0+r1,(din/2+r1)*kv,r1,0+r1,din/2*kv,0,(din/2+r1)*kv,-1*kv,1
Doc.ksArcByPoint B-r1,(din/2+r1)*kv,r1,B-r1,din/2*kv,B,(din/2+r1)*kv,1*kv,1
End Sub


'****************************************************

'///////////////Filling//////////////////////////////
Sub UpperHalfFilling(kv)
'inner ring
Doc.ksLineSeg 0,(din/2+r1)*kv,0,d1S/2*kv,1
Doc.ksLineSeg B,(din/2+r1)*kv,B,d1S/2*kv,1
Doc.ksArcByPoint B/2,0,d2S/2,B/2-C/2,ySL*kv,0,d1S/2*kv,1*kv,1
Doc.ksArcByPoint B/2,0,d2S/2,B,d1S/2*kv,B/2+C/2,ySL*kv,1*kv,1
If PAR_AXIS Then
	Doc.ksLineSeg 0,0,0,(din/2+r1)*kv,1
	Doc.ksLineSeg B,0,B,(din/2+r1)*kv,1
Else
	Doc.ksArcByPoint 0+r1,(din/2+r1)*kv,r1,0+r1,din/2*kv,0,(din/2+r1)*kv,-1*kv,1
	Doc.ksArcByPoint B-r1,(din/2+r1)*kv,r1,B-r1,din/2*kv,B,(din/2+r1)*kv,1*kv,1
	If version=1 Then
		Doc.ksLineSeg 0+r1,din/2*kv,B-r1,din/2*kv,1
	Else
		'line d with groove
		If rule Then
			Doc.ksLineSeg 0+r1,din/2*kv,xLbgInRIn,din/2*kv,1
			Doc.ksLineSeg xRbgInRIn,din/2*kv,B-r1,din/2*kv,1
			Doc.ksArcByPoint B/2,ycbgInRIn*kv,bg/2,xLbgInRIn,din/2*kv,xRbgInRIn,din/2*kv,-1*kv,1
		Else
			Doc.ksLineSeg 0+r1,din/2*kv,B-r1,din/2*kv,1
		End If
	End If
End If

'outer ring
Doc.ksLineSeg B/2-C/2,ySL*kv,B/2-C/2,(D/2-r)*kv,1
Doc.ksLineSeg B/2+C/2,ySL*kv,B/2+C/2,(D/2-r)*kv,1

If version<>3 OR rule=0 Then
	Doc.ksLineSeg B/2-C/2+r,D/2*kv,B/2+C/2-r,D/2*kv,1
Else
	Doc.ksLineSeg B/2-C/2+r,D/2*kv,xLbgOutROut,D/2*kv,1
	Doc.ksLineSeg xRbgOutROut,D/2*kv,B/2+C/2-r,D/2*kv,1
	Doc.ksArcByPoint B/2,ycbgOutROut*kv,bg/2,xLbgOutROut,D/2*kv,xRbgOutROut,D/2*kv,1*kv,1
End If

Doc.ksArcByPoint B/2-C/2+r,(D/2-r)*kv,r,B/2-C/2+r,D/2*kv,B/2-C/2,(D/2-r)*kv,1*kv,1
Doc.ksArcByPoint B/2+C/2-r,(D/2-r)*kv,r,B/2+C/2-r,D/2*kv,B/2+C/2,(D/2-r)*kv,-1*kv,1
End Sub

Sub BottomHalfFilling(kv)
'inner ring
Doc.ksLineSeg 0,(din/2+r1)*kv,0,d1/2*kv,1
Doc.ksLineSeg B,(din/2+r1)*kv,B,d1/2*kv,1

Doc.ksArcByPoint B/2,0,d2/2,B/2-C/2,yCL*kv,0,d1/2*kv,1*kv,1
Doc.ksArcByPoint B/2,0,d2/2,B,d1/2*kv,B/2+C/2,yCL*kv,1*kv,1


If PAR_AXIS Then
	Doc.ksLineSeg 0,0,0,(din/2+r1)*kv,1
	Doc.ksLineSeg B,0,B,(din/2+r1)*kv,1
Else
	Doc.ksArcByPoint 0+r1,(din/2+r1)*kv,r1,0+r1,din/2*kv,0,(din/2+r1)*kv,-1*kv,1
	Doc.ksArcByPoint B-r1,(din/2+r1)*kv,r1,B-r1,din/2*kv,B,(din/2+r1)*kv,1*kv,1
	If version=1 Then
		Doc.ksLineSeg 0+r1,din/2*kv,B-r1,din/2*kv,1
	Else
		'line d with groove
		If rule Then
			Doc.ksLineSeg 0+r1,din/2*kv,xLbgInRIn,din/2*kv,1
			Doc.ksLineSeg xRbgInRIn,din/2*kv,B-r1,din/2*kv,1
			Doc.ksArcByPoint B/2,ycbgInRIn*kv,bg/2,xLbgInRIn,din/2*kv,xRbgInRIn,din/2*kv,-1*kv,1
		Else
			Doc.ksLineSeg 0+r1,din/2*kv,B-r1,din/2*kv,1
		End If
	End If
End If

'outer ring
Doc.ksLineSeg B/2-C/2,yCL*kv,B/2-C/2,(D/2-r)*kv,1
Doc.ksLineSeg B/2+C/2,yCL*kv,B/2+C/2,(D/2-r)*kv,1
If version<>3 OR rule=0 Then
	Doc.ksLineSeg B/2-C/2+r,D/2*kv,B/2+C/2-r,D/2*kv,1
Else
	Doc.ksLineSeg B/2-C/2+r,D/2*kv,xLbgOutROut,D/2*kv,1
	Doc.ksLineSeg xRbgOutROut,D/2*kv,B/2+C/2-r,D/2*kv,1
	Doc.ksArcByPoint B/2,ycbgOutROut*kv,bg/2,xLbgOutROut,D/2*kv,xRbgOutROut,D/2*kv,1*kv,1
End If

Doc.ksArcByPoint B/2-C/2+r,(D/2-r)*kv,r,B/2-C/2+r,D/2*kv,B/2-C/2,(D/2-r)*kv,1*kv,1
Doc.ksArcByPoint B/2+C/2-r,(D/2-r)*kv,r,B/2+C/2-r,D/2*kv,B/2+C/2,(D/2-r)*kv,-1*kv,1
End Sub


'///////////////Filling//////////////////////////////


Sub BottomHalf_4(kv)
'inner ring

Doc.ksLineSeg 0,(din/2+r1)*kv,-l+r1,(din/2+r1)*kv,1
Doc.ksLineSeg B,(din/2+r1)*kv,B+l-r1,(din/2+r1)*kv,1
Doc.ksLineSeg -l,(din/2+2*r1)*kv,-l,(d1/2-r1)*kv,1
Doc.ksLineSeg B+l,(din/2+2*r1)*kv,B+l,(d1/2-r1)*kv,1
Doc.ksLineSeg 0,d1/2*kv,-l+r1,d1/2*kv,1
Doc.ksLineSeg B,d1/2*kv,B+l-r1,d1/2*kv,1

REM If version=1 OR rule=0 Then
If rule=0 Then
	Doc.ksArcByPoint B/2,0,d2/2,B,d1/2*kv,0,d1/2*kv,1*kv,1
	Doc.ksLineSeg 0+r1,din/2*kv,B-r1,din/2*kv,1
Else
	'ark d2 with groove
	Doc.ksArcByPoint B/2,0,d2/2,B,d1/2*kv,0,d1/2*kv,1*kv,1
	Doc.ksArcByPoint B/2,ycbgInROut*kv,bg/2,xLbgInROutB,yLbgInROutB*kv,xRbgInROutB,yRbgInROutB*kv,1*kv,1
	'line d with groove
	Doc.ksLineSeg 0+r1,din/2*kv,xLbgInRIn,din/2*kv,1
	Doc.ksLineSeg xRbgInRIn,din/2*kv,B-r1,din/2*kv,1
	Doc.ksArcByPoint B/2,ycbgInRIn*kv,bg/2,xLbgInRIn,din/2*kv,xRbgInRIn,din/2*kv,-1*kv,1
	
	'hole
	Doc.ksLineSeg B/2-dh/2,yLbgdhInRIn*kv,B/2-dh/2,yLbgdhInROut*kv,1
	Doc.ksLineSeg B/2+dh/2,yRbgdhInRIn*kv,B/2+dh/2,yRbgdhInROut*kv,1
	
	
	If PAR_AXIS Then
		Doc.ksLineSeg xLbgInRIn,0,xLbgInRIn,din/2*kv,1
		Doc.ksLineSeg xRbgInRIn,0,xRbgInRIn,din/2*kv,1
	End If
End If

Doc.ksArcByPoint -l+r1,(din/2+2*r1)*kv,r1,-l+r1,(din/2+r1)*kv,-l,(din/2+2*r1)*kv,-1*kv,1
Doc.ksArcByPoint B+l-r1,(din/2+2*r1)*kv,r1,B+l-r1,(din/2+r1)*kv,B+l,(din/2+2*r1)*kv,1*kv,1
Doc.ksArcByPoint -l+r1,(d1/2-r1)*kv,r1,-l+r1,(d1/2)*kv,-l,(d1/2-r1)*kv,1*kv,1
Doc.ksArcByPoint B+l-r1,(d1/2-r1)*kv,r1,B+l-r1,(d1/2)*kv,B+l,(d1/2-r1)*kv,-1*kv,1
Doc.ksLineSeg 0,(din/2+r1)*kv,0+r1,din/2*kv,1
Doc.ksLineSeg B,(din/2+r1)*kv,B-r1,din/2*kv,1

If PAR_AXIS Then
	Doc.ksLineSeg 0,0,0,(din/2+r1)*kv,1
	Doc.ksLineSeg B,0,B,(din/2+r1)*kv,1
	Doc.ksLineSeg 0+r1,0,0+r1,din/2*kv,1
	Doc.ksLineSeg B-r1,0,B-r1,din/2*kv,1
	
	Doc.ksLineSeg -l,0,-l,(din/2+2*r1)*kv,1
	Doc.ksLineSeg B+l,0,B+l,(din/2+2*r1)*kv,1
	Doc.ksLineSeg -l+r1,0,-l+r1,(din/2+r1)*kv,1
	Doc.ksLineSeg B+l-r1,0,B+l-r1,(din/2+r1)*kv,1
	
End If

'outer ring
Doc.ksLineSeg B/2-C/2,yCL*kv,B/2-C/2,(D/2-r)*kv,1
Doc.ksLineSeg B/2+C/2,yCL*kv,B/2+C/2,(D/2-r)*kv,1

Doc.ksLineSeg B/2-C/2+r,D/2*kv,xLbgOutROut,D/2*kv,1
Doc.ksLineSeg xRbgOutROut,D/2*kv,B/2+C/2-r,D/2*kv,1
Doc.ksArcByPoint B/2,ycbgOutROut*kv,bg/2,xLbgOutROut,D/2*kv,xRbgOutROut,D/2*kv,1*kv,1

Doc.ksArcByPoint B/2-C/2+r,(D/2-r)*kv,r,B/2-C/2+r,D/2*kv,B/2-C/2,(D/2-r)*kv,1*kv,1
Doc.ksArcByPoint B/2+C/2-r,(D/2-r)*kv,r,B/2+C/2-r,D/2*kv,B/2+C/2,(D/2-r)*kv,-1*kv,1

'hole
Doc.ksLineSeg B/2-dh/2,yLbgdhDnOutROut*kv,B/2-dh/2,yLbgdhOutROut*kv,1
Doc.ksLineSeg B/2+dh/2,yRbgdhDnOutROut*kv,B/2+dh/2,yRbgdhOutROut*kv,1

End Sub

Sub BottomHalfInRingHatch_4(kv)
'inner ring

Doc.ksLineSeg 0,(din/2+r1)*kv,-l+r1,(din/2+r1)*kv,1
Doc.ksLineSeg B,(din/2+r1)*kv,B+l-r1,(din/2+r1)*kv,1
Doc.ksLineSeg -l,(din/2+2*r1)*kv,-l,(d1/2-r1)*kv,1
Doc.ksLineSeg B+l,(din/2+2*r1)*kv,B+l,(d1/2-r1)*kv,1
Doc.ksLineSeg 0,d1/2*kv,-l+r1,d1/2*kv,1
Doc.ksLineSeg B,d1/2*kv,B+l-r1,d1/2*kv,1

REM If version=1 Then
If rule=0 Then
	Doc.ksArcByPoint B/2,0,d2/2,B,d1/2*kv,0,d1/2*kv,1*kv,1
	Doc.ksLineSeg 0+r1,din/2*kv,B-r1,din/2*kv,1
Else
	'ark d2 with groove
	Doc.ksArcByPoint B/2,0,d2/2,B,d1/2*kv,xRbgInROutB,yRbgInROutB*kv,1*kv,1
	Doc.ksArcByPoint B/2,0,d2/2,xLbgInROutB,yLbgInROutB*kv,0,d1/2*kv,1*kv,1
	Doc.ksArcByPoint B/2,ycbgInROut*kv,bg/2,xLbgInROutB,yLbgInROutB*kv,B/2-dh/2,yLbgdhInROut*kv,1*kv,1
	Doc.ksArcByPoint B/2,ycbgInROut*kv,bg/2,B/2+dh/2,yRbgdhInROut*kv,xRbgInROutB,yRbgInROutB*kv,1*kv,1
	'line d with groove
	Doc.ksLineSeg 0+r1,din/2*kv,xLbgInRIn,din/2*kv,1
	Doc.ksLineSeg xRbgInRIn,din/2*kv,B-r1,din/2*kv,1
	Doc.ksArcByPoint B/2,ycbgInRIn*kv,bg/2,xLbgInRIn,din/2*kv,B/2-dh/2,yLbgdhInRIn*kv,-1*kv,1
	Doc.ksArcByPoint B/2,ycbgInRIn*kv,bg/2,B/2+dh/2,yRbgdhInRIn*kv,xRbgInRIn,din/2*kv,-1*kv,1
	
	'hole
	Doc.ksLineSeg B/2-dh/2,yLbgdhInRIn*kv,B/2-dh/2,yLbgdhInROut*kv,1
	Doc.ksLineSeg B/2+dh/2,yRbgdhInRIn*kv,B/2+dh/2,yRbgdhInROut*kv,1
End If

Doc.ksArcByPoint -l+r1,(din/2+2*r1)*kv,r1,-l+r1,(din/2+r1)*kv,-l,(din/2+2*r1)*kv,-1*kv,1
Doc.ksArcByPoint B+l-r1,(din/2+2*r1)*kv,r1,B+l-r1,(din/2+r1)*kv,B+l,(din/2+2*r1)*kv,1*kv,1
Doc.ksArcByPoint -l+r1,(d1/2-r1)*kv,r1,-l+r1,(d1/2)*kv,-l,(d1/2-r1)*kv,1*kv,1
Doc.ksArcByPoint B+l-r1,(d1/2-r1)*kv,r1,B+l-r1,(d1/2)*kv,B+l,(d1/2-r1)*kv,-1*kv,1
Doc.ksLineSeg 0,(din/2+r1)*kv,0+r1,din/2*kv,1
Doc.ksLineSeg B,(din/2+r1)*kv,B-r1,din/2*kv,1
End Sub

Sub BottomHalfOutHatch_4(kv)
'outer ring
Doc.ksLineSeg B/2-C/2,yCL*kv,B/2-C/2,(D/2-r)*kv,1
Doc.ksLineSeg B/2+C/2,yCL*kv,B/2+C/2,(D/2-r)*kv,1
Doc.ksArcByPoint B/2,0,d2/2,B/2+C/2,yCL*kv,B/2+dh/2,yRbgdhDnOutROut*kv,1*kv,1
Doc.ksArcByPoint B/2,0,d2/2,B/2-dh/2,yLbgdhDnOutROut*kv,B/2-C/2,yCL*kv,1*kv,1

Doc.ksLineSeg B/2-C/2+r,D/2*kv,xLbgOutROut,D/2*kv,1
Doc.ksLineSeg xRbgOutROut,D/2*kv,B/2+C/2-r,D/2*kv,1
Doc.ksArcByPoint B/2,ycbgOutROut*kv,bg/2,xLbgOutROut,D/2*kv,B/2-dh/2,yLbgdhOutROut*kv,1*kv,1
Doc.ksArcByPoint B/2,ycbgOutROut*kv,bg/2,B/2+dh/2,yRbgdhOutROut*kv,xRbgOutROut,D/2*kv,1*kv,1

Doc.ksArcByPoint B/2-C/2+r,(D/2-r)*kv,r,B/2-C/2+r,D/2*kv,B/2-C/2,(D/2-r)*kv,1*kv,1
Doc.ksArcByPoint B/2+C/2-r,(D/2-r)*kv,r,B/2+C/2-r,D/2*kv,B/2+C/2,(D/2-r)*kv,-1*kv,1

'hole
Doc.ksLineSeg B/2-dh/2,yLbgdhDnOutROut*kv,B/2-dh/2,yLbgdhOutROut*kv,1
Doc.ksLineSeg B/2+dh/2,yRbgdhDnOutROut*kv,B/2+dh/2,yRbgdhOutROut*kv,1

End Sub

Sub BottomHalfFilling_4(kv)
'inner ring

Doc.ksLineSeg -l,(din/2+2*r1)*kv,-l,(d1/2-r1)*kv,1
Doc.ksLineSeg B+l,(din/2+2*r1)*kv,B+l,(d1/2-r1)*kv,1
Doc.ksLineSeg 0,d1/2*kv,-l+r1,d1/2*kv,1
Doc.ksLineSeg B,d1/2*kv,B+l-r1,d1/2*kv,1

REM If version=1 Then
	REM Doc.ksArcByPoint B/2,0,d2/2,B,d1/2*kv,0,d1/2*kv,1*kv,1
	REM Doc.ksLineSeg 0+r1,din/2*kv,B-r1,din/2*kv,1
REM Else
	'ark d2 with groove
	Doc.ksArcByPoint B/2,0,d2/2,B,d1/2*kv,B/2+C/2,yCL*kv,1*kv,1
	Doc.ksArcByPoint B/2,0,d2/2,B/2-C/2,yCL*kv,0,d1/2*kv,1*kv,1
	'line d with groove
	If PAR_AXIS=0 Then
		Doc.ksLineSeg 0,(din/2+r1)*kv,-l+r1,(din/2+r1)*kv,1
		Doc.ksLineSeg B,(din/2+r1)*kv,B+l-r1,(din/2+r1)*kv,1
		If rule Then
			Doc.ksLineSeg 0+r1,din/2*kv,xLbgInRIn,din/2*kv,1
			Doc.ksLineSeg xRbgInRIn,din/2*kv,B-r1,din/2*kv,1
			Doc.ksArcByPoint B/2,ycbgInRIn*kv,bg/2,xLbgInRIn,din/2*kv,xRbgInRIn,din/2*kv,-1*kv,1
		Else
			Doc.ksLineSeg 0+r1,din/2*kv,B-r1,din/2*kv,1
		End If
		Doc.ksArcByPoint -l+r1,(din/2+2*r1)*kv,r1,-l+r1,(din/2+r1)*kv,-l,(din/2+2*r1)*kv,-1*kv,1
		Doc.ksArcByPoint B+l-r1,(din/2+2*r1)*kv,r1,B+l-r1,(din/2+r1)*kv,B+l,(din/2+2*r1)*kv,1*kv,1
		Doc.ksLineSeg 0,(din/2+r1)*kv,0+r1,din/2*kv,1
		Doc.ksLineSeg B,(din/2+r1)*kv,B-r1,din/2*kv,1
	End If
REM End If

Doc.ksArcByPoint -l+r1,(d1/2-r1)*kv,r1,-l+r1,(d1/2)*kv,-l,(d1/2-r1)*kv,1*kv,1
Doc.ksArcByPoint B+l-r1,(d1/2-r1)*kv,r1,B+l-r1,(d1/2)*kv,B+l,(d1/2-r1)*kv,-1*kv,1

If PAR_AXIS Then
	Doc.ksLineSeg -l,0,-l,(din/2+2*r1)*kv,1
	Doc.ksLineSeg B+l,0,B+l,(din/2+2*r1)*kv,1
End If

'outer ring
Doc.ksLineSeg B/2-C/2,yCL*kv,B/2-C/2,(D/2-r)*kv,1
Doc.ksLineSeg B/2+C/2,yCL*kv,B/2+C/2,(D/2-r)*kv,1

Doc.ksLineSeg B/2-C/2+r,D/2*kv,xLbgOutROut,D/2*kv,1
Doc.ksLineSeg xRbgOutROut,D/2*kv,B/2+C/2-r,D/2*kv,1
Doc.ksArcByPoint B/2,ycbgOutROut*kv,bg/2,xLbgOutROut,D/2*kv,xRbgOutROut,D/2*kv,1*kv,1

Doc.ksArcByPoint B/2-C/2+r,(D/2-r)*kv,r,B/2-C/2+r,D/2*kv,B/2-C/2,(D/2-r)*kv,1*kv,1
Doc.ksArcByPoint B/2+C/2-r,(D/2-r)*kv,r,B/2+C/2-r,D/2*kv,B/2+C/2,(D/2-r)*kv,-1*kv,1
End Sub

Sub LeftView
Select Case version
Case 1,2,3
If version=1 Then
	Doc.ksCircle 0,0,D/2,1
	Doc.ksCircle 0,0,D/2-r,1
	Doc.ksCircle 0,0,din/2,1
	Doc.ksCircle 0,0,din/2+r1,1
	Doc.ksCircle 0,0,d2/2,4
	Doc.ksArcByPoint 0,0,d1/2,xrcSpV,-d1s/2,xrcSpV,d1s/2,1,1
	Doc.ksArcByPoint 0,0,yCL,xCyCL,-yCyCL,xCyCL,yCyCL,1,1
Else
	For i=1 To -1 Step -2
		WaveLine i,2
	Next
	Doc.ksCircle 0,0,D/2,1
	Doc.ksArcByPoint 0,0,D/2-r,xDr_W,yDr_W,xDr_W,-yDr_W,1,1
	Doc.ksCircle 0,0,din/2,1
	Doc.ksArcByPoint 0,0,din/2+r1,xdinr_W,ydinr_W,xdinr_W,-ydinr_W,1,1
	Doc.ksArcByPoint 0,0,d2/2,xd2_W,yd2_W,xd2_W,-yd2_W,1,4
	Doc.ksArcByPoint 0,0,d1/2,xrcSpV,-d1s/2,xd1_W,-yd1_W,1,1
	Doc.ksArcByPoint 0,0,d1/2,xd1_W,yd1_W,xrcSpV,d1s/2,1,1
	Doc.ksArcByPoint 0,0,yCL,xCyCL,-yCyCL,xCL_W,-yCL_W,1,1
	Doc.ksArcByPoint 0,0,yCL,xCL_W,yCL_W,xCyCL,yCyCL,1,1
	'inside local cut
	rbg=b_ellips (bg/2,dh/2)
	REM Doc.ksLineSeg xdhIn_H,ydhIn_H,xdhInOut_H,ydhInOut_H,1
	REM Doc.ksLineSeg xdhIn_H,-ydhIn_H,xdhInOut_H,-ydhInOut_H,1
	If rule Then
		Doc.ksLineSeg xdhIn_H,ydhIn_H,xdhInOut_H,ydhInOut_H,1
		Doc.ksLineSeg xdhIn_H,-ydhIn_H,xdhInOut_H,-ydhInOut_H,1
		Doc.ksArcByPoint 0,0,ycbgInRIn+bg/2,xbgIn_W,-ybgIn_W,xdhIn_H,-ydhIn_H,1,1
		Doc.ksArcByPoint 0,0,ycbgInRIn+bg/2,xdhIn_H,ydhIn_H,xbgIn_W,ybgIn_W,1,1
		Doc.ksArcByPoint xdhIn_H+bg/2-rbg,0,bg/2,xdhIn_H,ydhIn_H,xdhIn_H,-ydhIn_H,1,1
	Else
		Doc.ksLineSeg xdin_H,ydin_H,xdhInOut_H,ydhInOut_H,1
		Doc.ksLineSeg xdin_H,-ydin_H,xdhInOut_H,-ydhInOut_H,1
	End If
	Doc.ksArcByPoint 0,0,ycbgInROut-bg/2,xdhInOut_H,ydhInOut_H,xbgInOut_W,ybgInOut_W,1,1
	Doc.ksArcByPoint 0,0,ycbgInROut-bg/2,xbgInOut_W,-ybgInOut_W,xdhInOut_H,-ydhInOut_H,1,1
	Doc.ksArcByPoint xdhInOut_H-bg/2+rbg,0,bg/2,xdhInOut_H,-ydhInOut_H,xdhInOut_H,ydhInOut_H,1,1
	REM Doc.ksArcByPoint xdhIn_H+bg/2-rbg,0,bg/2,xdhIn_H,ydhIn_H,xdhIn_H,-ydhIn_H,1,1

	'hatch for v2 and partially v3 (inner ring)
	For i=1 To -1 Step -2
		REM Doc.ksMtr D*0.55,0,0,1,1
		Doc.ksHatch 0, -hatchAngle, hatchStep, (xdhIn_H+xdhInOut_H)/2, (ydhIn_H*i+ydhInOut_H*i)/2, 0 
			WaveLine i,1
			If rule Then
				REM Doc.ksArcByPoint 0,0,ycbgInRIn+bg/2,xdhIn_H,ydhIn_H*i,xbgIn_W,ybgIn_W*i,1*i,1
				Doc.ksArcByPoint 0,0,ycbgInRIn+bg/2,xdhIn_H,ydhIn_H*i,0,(ycbgInRIn+bg/2)*i,1*i,1
				REM Doc.ksArcByPoint 0,0,ycbgInROut-bg/2,xdhInOut_H,ydhInOut_H*i,xbgInOut_W,ybgInOut_W*i,1*i,1
				Doc.ksArcByPoint 0,0,ycbgInROut-bg/2,xdhInOut_H,ydhInOut_H*i,0,(ycbgInROut-bg/2)*i,1*i,1
				'It is curious thing - if hatchAngle < 0 and hatch area lays under axis X the contour of that area have to be pointed with arcs stick out of wave line (Bezier). Otherwise hatch will turn out of the area. In other case (hatchAngle > 0, area above axis X) the area have to be pointed point in point.
				Doc.ksLineSeg xdhIn_H,ydhIn_H*i,xdhInOut_H,ydhInOut_H*i,1
			Else
				Doc.ksArcByPoint 0,0,din/2,xdin_H,ydin_H*i,0,(ycbgInRIn+bg/2)*i,1*i,1
				Doc.ksArcByPoint 0,0,ycbgInROut-bg/2,xdhInOut_H,ydhInOut_H*i,0,(ycbgInROut-bg/2)*i,1*i,1
				Doc.ksLineSeg xdin_H,ydin_H*i,xdhInOut_H,ydhInOut_H*i,1
			End If
		Doc.ksEndObj
		REM Doc.ksDeleteMtr
	Next


If version=2 Then
Doc.ksArcByPoint 0,0,d2/2,xd2_W,-yd2_W,xd2_W,yd2_W,1,1
REM Doc.ksArcByPoint 0,0,ycbgOutROut-bg/2,xbgOut_W,-ybgOut_W,xbgOut_W,ybgOut_W,1,1
	
'hatch for v2 outer ring
REM Doc.ksMtr D*0.55,0,0,1,1
Doc.ksHatch 0,hatchAngle,hatchStep,(d2/2+ycbgOutROut-bg/2)/2,0,0
	WaveLine 1,1
	WaveLine -1,1
	REM Doc.ksArcByPoint 0,0,d2/2,0,-d2/2,0,d2/2,1,1
	REM Doc.ksArcByPoint 0,0,ycbgOutROut-bg/2,0,(ycbgOutROut-bg/2)*-1,0,(ycbgOutROut-bg/2),1,1
	
	Doc.ksArcByPoint 0,0,d2/2,xd2_W,yd2_W*-1,xd2_W,yd2_W,1,1
	REM Doc.ksArcByPoint 0,0,d2/2,xd2_H,yd2_H*i,0,d2/2*i,1*i,1
	REM Doc.ksArcByPoint 0,0,ycbgOutROut-bg/2,xbgOut_W,ybgOut_W*-1,xbgOut_W,ybgOut_W,1,1
	Doc.ksArcByPoint 0,0,D/2,xD_W,yD_W*-1,xD_W,yD_W,1,1
Doc.ksEndObj
REM Doc.ksDeleteMtr
Else
Doc.ksArcByPoint 0,0,d2/2,xd2_H,yd2_H,xd2_W,yd2_W,1,1
Doc.ksArcByPoint 0,0,d2/2,xd2_H,-yd2_H,xd2_W,-yd2_W,-1,1
If rule Then
	Doc.ksArcByPoint 0,0,ycbgOutROut-bg/2,xdhOut_H,ydhOut_H,xbgOut_W,ybgOut_W,1,1
	Doc.ksArcByPoint 0,0,ycbgOutROut-bg/2,xdhOut_H,-ydhOut_H,xbgOut_W,-ybgOut_W,-1,1
	Doc.ksLineSeg xd2_H,yd2_H,xdhOut_H,ydhOut_H,1
	Doc.ksLineSeg xd2_H,-yd2_H,xdhOut_H,-ydhOut_H,1
	Doc.ksArcByPoint xdhOut_H-bg/2+rbg,0,bg/2,xdhOut_H,-ydhOut_H,xdhOut_H,ydhOut_H,1,1
Else
	Doc.ksLineSeg xd2_H,yd2_H,xD_H,yD_H,1
	Doc.ksLineSeg xd2_H,-yd2_H,xD_H,-yD_H,1
End If
REM Doc.ksLineSeg xd2_H,yd2_H,xdhOut_H,ydhOut_H,1
REM Doc.ksLineSeg xd2_H,-yd2_H,xdhOut_H,-ydhOut_H,1

REM Doc.ksArcByPoint xdhOut_H-bg/2+rbg,0,bg/2,xdhOut_H,-ydhOut_H,xdhOut_H,ydhOut_H,1,1

rd2=b_ellips(d2/2,dh/2)
Doc.ksArcByPoint xd2_H+d2/2-rd2,0,d2/2,xd2_H,yd2_H,xd2_H,-yd2_H,1,1

'hatch for v3
REM For i=1 To -1 Step -2
	REM REM Doc.ksMtr D*0.55,0,0,1,1
	REM Doc.ksHatch 0,hatchAngle,hatchStep,(xd2_H+xbgOut_W)/2,(yd2_H*i+ybgOut_W*i)/2,0
		REM WaveLine i,1
		REM Doc.ksLineSeg xd2_H,yd2_H*i,xdhOut_H,ydhOut_H*i,1

		REM Doc.ksArcByPoint 0,0,d2/2,xd2_H,yd2_H*i,xd2_W,yd2_W*i,1*i,1
		REM REM Doc.ksArcByPoint 0,0,d2/2,xd2_H,yd2_H*i,0,d2/2*i,1*i,1
		REM Doc.ksArcByPoint 0,0,ycbgOutROut-bg/2,xdhOut_H,ydhOut_H*i,xbgOut_W,ybgOut_W*i,1*i,1
		REM REM Doc.ksArcByPoint 0,0,ycbgOutROut-bg/2,xdhOut_H,ydhOut_H*i,0,(ycbgOutROut-bg/2)*i,1*i,1
	REM Doc.ksEndObj
	REM REM Doc.ksDeleteMtr
REM Next
'???????????????
'hatch for v3
'It is curious thing - if hatch area lays under axis X the contour of that area have to be pointed with arcs stick out of wave line (Bezier). Otherwise hatch will turn out of the area in some cases (e.g. G-5-16-9 or G-8-19-11). In other case (area above axis X) the area have to be pointed point in point.

REM For i=1 To -1 Step -2
	REM Doc.ksMtr D*0.55,0,0,1,1
	Doc.ksHatch 0,hatchAngle,hatchStep,(xd2_H+xbgOut_W)/2,(yd2_H*i+ybgOut_W*i)/2,0
i=1
	WaveLine i,1
		REM Doc.ksLineSeg xd2_H,yd2_H*i,xdhOut_H,ydhOut_H*i,1

		Doc.ksArcByPoint 0,0,d2/2,xd2_H,yd2_H*i,xd2_W,yd2_W*i,1*i,1
		REM Doc.ksArcByPoint 0,0,d2/2,xd2_H,yd2_H*i,0,d2/2*i,1*i,1
		If rule Then
			Doc.ksLineSeg xd2_H,yd2_H*i,xdhOut_H,ydhOut_H*i,1
			Doc.ksArcByPoint 0,0,ycbgOutROut-bg/2,xdhOut_H,ydhOut_H*i,xbgOut_W,ybgOut_W*i,1*i,1
		Else
			Doc.ksLineSeg xd2_H,yd2_H*i,xD_H,yD_H*i,1
			Doc.ksArcByPoint 0,0,D/2,xD_H,yD_H*i,xD_W,yD_W*i,1*i,1
		End If
		REM Doc.ksArcByPoint 0,0,ycbgOutROut-bg/2,xdhOut_H,ydhOut_H*i,0,(ycbgOutROut-bg/2)*i,1*i,1
i=-1
		WaveLine i,1
		REM Doc.ksLineSeg xd2_H,yd2_H*i,xdhOut_H,ydhOut_H*i,1

		REM Doc.ksArcByPoint 0,0,d2/2,xd2_H,yd2_H*i,xd2_W,yd2_W*i,1*i,1
		Doc.ksArcByPoint 0,0,d2/2,xd2_H,yd2_H*i,0,d2/2*i,1*i,1
		REM Doc.ksArcByPoint 0,0,ycbgOutROut-bg/2,xdhOut_H,ydhOut_H*i,xbgOut_W,ybgOut_W*i,1*i,1
		If rule Then
			Doc.ksLineSeg xd2_H,yd2_H*i,xdhOut_H,ydhOut_H*i,1
			Doc.ksArcByPoint 0,0,ycbgOutROut-bg/2,xdhOut_H,ydhOut_H*i,0,(ycbgOutROut-bg/2)*i,1*i,1
		Else
			Doc.ksLineSeg xd2_H,yd2_H*i,xD_H,yD_H*i,1
			Doc.ksArcByPoint 0,0,D/2,xD_H,yD_H*i,0,D/2*i,1*i,1
		End If
	Doc.ksEndObj
	REM Doc.ksDeleteMtr
REM Next
'???????????????
End If 'version=2 or no
End If 'version=1 or no
Doc.ksArcByPoint 0,0,yCL,-xCyCL,yCyCL,-xCyCL,-yCyCL,1,1
Doc.ksArcByPoint 0,0,rc,xrcCL,yrcCL,-xrcCL,yrcCL,1,1
Doc.ksArcByPoint 0,0,rc,-xrcCL,-yrcCL,xrcCL,-yrcCL,1,1
Doc.ksLineSeg -D*0.2,yCyCL,-D*0.2,yrcCL,1
Doc.ksLineSeg -D*0.2,-yCyCL,-D*0.2,-yrcCL,1
Doc.ksLineSeg D*0.2,yCyCL,D*0.2,yrcCL,1
Doc.ksLineSeg D*0.2,-yCyCL,D*0.2,-yrcCL,1


If yrcSpH>0 Then
	Doc.ksLineSeg -xrcSpH,yrcSpH,xrcSpH,yrcSpH,1
	Doc.ksLineSeg -xrcSpH,-yrcSpH,xrcSpH,-yrcSpH,1
End If
Doc.ksLineSeg xrcSpV,d1s/2,-xrcSpV,d1s/2,1
Doc.ksLineSeg xrcSpV,-d1s/2,-xrcSpV,-d1s/2,1
Doc.ksLineSeg -xrcSpV,d1s/2,-xrcSpV,yrcSpV,1
Doc.ksLineSeg -xrcSpV,-d1s/2,-xrcSpV,-yrcSpV,1
Doc.ksLineSeg xrcSpV,-d1s/2,xrcSpV,-yrcSpV,1
Doc.ksLineSeg xrcSpV,d1s/2,xrcSpV,yrcSpV,1

Doc.ksArcByPoint 0,0,d1/2,-xrcSpV,d1s/2,-xrcSpV,-d1s/2,1,1

Case 4
	Doc.ksCircle 0,0,D/2,1
	Doc.ksCircle 0,0,D/2-r,1
	Doc.ksCircle 0,0,din/2,1
	Doc.ksCircle 0,0,yCL,1
	Doc.ksCircle 0,0,din/2+r1,1
	Doc.ksCircle 0,0,din/2+r1*2,1
	Doc.ksCircle 0,0,d1/2,1
	Doc.ksCircle 0,0,d1/2-r1,1
	REM Doc.ksCircle 0,0,d2/2,4
End Select
'************Filling********************
REM Doc.ksMtr B*1.5,0,0,1,1
Doc.ksColouring &HFE000000
	Doc.ksCircle 0,0,D/2,1
	Doc.ksCircle 0,0,din/2,1
Doc.ksEndObj
REM Doc.ksDeleteMtr
'************Filling********************
If PAR_AXIS Then
	Doc.ksLineSeg -D/2-ax,0,D/2+ax,0,3
	Doc.ksLineSeg 0,-D/2-ax,0,D/2+ax,3
End If
End Sub

Sub Script_Onload
REM Doc.ksPoint 0,0,1
REM CharacterPoints
If version <4 Then
CharacterPoints
If SIDE=GCV_FRONT Then
UpperHalf 1
BottomHalf -1
If version>1 AND PAR_AXIS Then Doc.ksCircle B/2,0,dh/2,1

REM Doc.ksMtr B*1.5,0,0,1,1
Doc.ksHatch 0, hatchAngle, hatchStep, 0, 0, 0 
	UpperHalfOuterRingHatch 1
	BottomHalfOuterRingHatch -1
Doc.ksEndObj
REM Doc.ksDeleteMtr

REM Doc.ksMtr B*1.5,0,0,1,1
Doc.ksHatch 0, -hatchAngle, hatchStep, 0, 0, 0 
	UpperHalfInnerRingHatch 1
	BottomHalfInnerRingHatch -1
Doc.ksEndObj
REM Doc.ksDeleteMtr

REM Doc.ksMtr B*1.5,0,0,1,1
Doc.ksColouring &HFE000000
	UpperHalfFilling 1
	BottomHalfFilling -1
	If version=3 AND PAR_AXIS Then Doc.ksCircle B/2,0,dh/2,1
Doc.ksEndObj
REM Doc.ksDeleteMtr

If PAR_AXIS Then
	Doc.ksLineSeg 0-ax,0,B+ax,0,3
	Doc.ksLineSeg B/2,-D/2-ax,B/2,D/2+ax,3
End If
Else 'SIDE=GCV_LEFT
LeftView
End If 'SIDE
Else 'version=4
'*******************version 4******************************
alfad=5
alfa=alfad/180*pi
B4=C+(sin(alfa/2)*cos(asin(C/d2)-(pi-alfa)/2+pi/2)*d2)*2
l=(B-B4)/2
'*******************version 4******************************
Btmp=B
B=B4
CharacterPoints
If SIDE=GCV_FRONT Then
Doc.ksMtr l,0,0,1,1
	BottomHalf_4 1
	BottomHalf_4 -1
	REM Doc.ksMtr B+3*l,0,0,1,1
	Doc.ksHatch 0, hatchAngle, hatchStep, 0, 0, 0 
		BottomHalfOutHatch_4 1
		BottomHalfOutHatch_4 -1
	Doc.ksEndObj
	REM Doc.ksDeleteMtr
	REM Doc.ksMtr B+3*l,0,0,1,1
	Doc.ksHatch 0, -hatchAngle, hatchStep, 0, 0, 0 
		BottomHalfInRingHatch_4 1
		BottomHalfInRingHatch_4 -1
	Doc.ksEndObj
	REM Doc.ksDeleteMtr
	REM Doc.ksMtr B+3*l,0,0,1,1
	Doc.ksColouring &HFE000000
		BottomHalfFilling_4 1
		BottomHalfFilling_4 -1
	Doc.ksEndObj
	REM Doc.ksDeleteMtr
Doc.ksDeleteMtr
B=Btmp
If PAR_AXIS Then
	Doc.ksLineSeg 0-ax,0,B+ax,0,3
	Doc.ksLineSeg B/2,-D/2-ax,B/2,D/2+ax,3
End If
Else 'SIDE=GCV_LEFT
LeftView
End If 'SIDE
End If


End Sub

'crossing of arc with line
Sub GetIntersectArcLin(ArcX,ArcY,ArcR,ArcAng1,ArcAng2,n,LineX,LineY,Angle,PointX1,PointY1)
   PointX1 = 0
   PointY1 = 0
   Math.ksIntersectArcLin ArcX,ArcY,ArcR,ArcAng1,ArcAng2,n,LineX,LineY,Angle,iDA
   If iDA.ksGetArrayCount > 0 then
    iDA.ksGetArrayItem 0,iPoint
      PointX1 = iPoint.x
      PointY1 = iPoint.y
	  REM kompas.ksmessage ("iPoint.x="&iPoint.x&" iPoint.y="&iPoint.y)
    iDa.ksClearArray
   End If
End Sub

'arcs crossing
Sub GetIntersectArcArc(xac,yac,rada,fa1,fa2,directa,xbc,ybc,radb,fb1,fb2,directb,fArc,PX1,PointY1)

Math.ksIntersectArcArc xac,yac,rada,fa1,fa2,directa,xbc,ybc,radb,fb1,fb2,directb,iDA
fArc=iDA.ksGetArrayCount
If fArc > 0 then
	iDA.ksGetArrayItem 0, iPoint
	PX1 = iPoint.x
	PointY1 = iPoint.y
	iDa.ksClearArray
End If
End Sub

Function asin(a)
	asin=atn(a/sqr(1-a^2))
End Function

Sub DrawHalfEllips (xe,ye,ad,bd,ae1,ae2,dir,angle)
elpc.init
elpc.a = ad
elpc.b = bd
elpc.anglefirst = ae1
elpc.anglesecond = ae2
elpc.angle = angle
elpc.direction = dir
elpc.style = 1
elpc.xc = xe
elpc.yc = ye
Doc.ksEllipseArc elpc
End Sub

Function b_ellips(RL,r)
	b_ellips=RL-sqr(RL^2-r^2)
End Function

Sub WaveLine(i,style)
Doc.ksBezier 0,style
	Doc.ksPoint xdin_W,ydin_W*i,0
	If xbgIn_W>xdinr_W Then
		Doc.ksPoint xdinr_W,ydinr_W*i,0
		Doc.ksPoint xbgIn_W,ybgIn_W*i,0
	Else
		Doc.ksPoint xbgIn_W,ybgIn_W*i,0
		Doc.ksPoint xdinr_W,ydinr_W*i,0
	End If
	Doc.ksPoint xd1_W,yd1_W*i,0
	Doc.ksPoint xCL_W,yCL_W*i,0
	Doc.ksPoint xbgInOut_W,ybgInOut_W*i,0
	Doc.ksPoint xd2_W,yd2_W*i,0
	If xDr_W>xbgOut_W Then
		Doc.ksPoint xbgOut_W,ybgOut_W*i,0
		Doc.ksPoint xDr_W,yDr_W*i,0
	Else
		Doc.ksPoint xDr_W,yDr_W*i,0
		Doc.ksPoint xbgOut_W,ybgOut_W*i,0
	End If
	Doc.ksPoint xD_W,yD_W*i,0
Doc.ksEndObj
End Sub
